<template>
  <a-modal :open="open" width="640px" :title="rowData.id ? '修改模板分类' : '新增模板分类'" centered ok-text="保存" @ok="onOk" @cancel="onCancel">
    <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '120px' } }">
      <a-form-item label="医技分类" class="w-full mt-2"> {{ formState.firstNodeName }} </a-form-item>
      <a-form-item label="上级目录" class="w-full" v-bind="validateInfos.id">
        <a-tree-select
          v-model:value="formState.parentTpCategoryCode"
          :loading="loading"
          style="width: 100%"
          :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
          allow-clear
          :disabled="!!rowData.parentTpCategoryCode"
          tree-default-expand-all
          :tree-data="treeData"
          tree-node-filter-prop="label"
          placeholder="请选择"
          :field-names="{
            children: 'subList',
            label: 'name',
            key: 'code',
            value: 'code'
          }"
        />
      </a-form-item>
      <a-form-item label="模板分类名称" v-bind="validateInfos.tpCategoryName" class="w-full">
        <a-input v-model:value="formState.tpCategoryName" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="排序" v-bind="validateInfos.outorder" class="w-full">
        <a-input v-model:value="formState.outorder" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
// import useStore from '../store'
// import { post } from '@/utils/request'
// const store = useStore()
const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})

const loading = ref(false)
const treeData = ref([])
const formState = ref({
  id: undefined,
  categroyName: ''
})
const rulesState = ref({
  id: [{ required: true, message: '请选择上级目录' }],
  categroyName: [{ required: true, message: '请输入病历文书名称' }]
})
const { resetFields, validateInfos } = useForm(formState, rulesState)

// 获取文书
const getCategoryTree = async () => {
  loading.value = true
  const { code, data, msg } = await post('/ris/template/getTemplateCategoryList', {
    categoryCode: props.rowData.categoryCode,
    searchWord: ''
  })
  if (code !== 0) return message.error(msg)
  loading.value = false
  treeData.value = filterNodesByTemplate(data)
}

// 保存
const onOk = async () => {
  const { code, msg } = await post('/ris/template/doSaveTemplateCategory', formState.value)
  if (code !== 0) return message.error(msg)
  message.success('保存成功')
  onCancel()
  emit('success')
}

// 关闭
const onCancel = () => {
  emit('update:open', false)
  resetFields()
}

// 过滤掉 template 字段为 true 的节点及其子节点
const filterNodesByTemplate = (nodes = []) => {
  const filteredNodes = []
  nodes.forEach((node) => {
    if (node.parentCode !== '0' && node.nodeType === 0) {
      return
    }
    if (node.subList && node.subList.length > 0) {
      node.subList = filterNodesByTemplate(node.subList)
    }
    filteredNodes.push(node)
  })

  return filteredNodes
}
watch(
  () => props.open,
  (val) => {
    if (val) {
      if (props.rowData.isAdd) {
        //新增
        formState.value = {
          firstNodeName: props.rowData.firstNodeName,
          categoryCode: props.rowData.categoryCode,
          parentTpCategoryCode: props.rowData.parentCode === '0' ? props.rowData.firstNodeCode : props.rowData.code
        }
      } else {
        //编辑
        formState.value = {
          firstNodeName: props.rowData.firstNodeName,
          categoryCode: props.rowData.categoryCode,
          parentTpCategoryCode: props.rowData.parentCode === '0' ? props.rowData.firstNodeCode : props.rowData.parentCode,
          tpCategoryCode: props.rowData.code,
          tpCategoryName: props.rowData.name,
          memo: props.rowData.memo
        }
      }
      getCategoryTree()
    } else {
      resetFields()
    }
  }
)
</script>
